#include <Arduino.h>
#include <btMusicBox.h>
#include <chrono> //C++时间戳

// Sets the name of the audio device
btMusicBox music = btMusicBox("MusicBox");
String last_title = "";
String last_artist = "";
uint8_t flag_first = 0;
uint8_t music_lyrics[100] = {0};
uint8_t music_lyrics1[100] = {0};
std::chrono::steady_clock::time_point _audioUpdate;

void audioLyricsupdate(uint16_t duration)
{
    int gbk_len = 0;
    auto now = std::chrono::steady_clock::now();
    int64_t dt = std::chrono::duration_cast<std::chrono::milliseconds>(now - _audioUpdate).count();
    if (dt >= duration)
    {
        _audioUpdate = now;
        music.updateMeta();
        if (music.artist.compareTo(last_artist) != 0)
        {
            Serial.print("Artist: ");
            Serial.println(music.artist);
            Serial.print("Album: ");
            Serial.println(music.album);
            Serial.print("Genre: ");
            Serial.println(music.genre);
            last_artist = music.artist;
        }
        if (music.title.compareTo(last_title) != 0)
        {
            Serial.print("歌词: ");
            Serial.println(music.title);
            // if(-1 != SwitchToGbk((const uint8_t *)music.title.c_str(),strlen(music.title.c_str()),music_lyrics1,&gbk_len)){
            //     memcpy(music_lyrics,music_lyrics1,sizeof(music_lyrics1));
            // }
            last_title = music.title;
        }
    }
}

void setup()
{
    Serial.begin(115200);
    // Wire.begin(I2C_SDA, I2C_SCL, I2C_FREQ);
    // Audio_codeC(ES8388);
    pinMode(13, OUTPUT);
    music.begin();
    music.I2S(I2S_BCK, I2S_DOUT, I2S_WS);
    music.reconnect(); // Re-connects to last connected device
                       // attendpaintingclass(); // attendpaintingclass
}
void loop()
{
    delay(10);
    // ADC_KeyScan();
    // painting->mainLoop();   // paintingclass
    audioLyricsupdate(100); // update musicLyrics
}
